<?php  
/*
*	Kleophatra - Open Source Content Management System
*	------------------------------------------------------------------------
*	Copyright (C) 2007-2010, Sergio Milardovich <smilardovich@yahoo.com.ar>
*	This program is Free Software.
*	kleoconfig.php: Kleophatra configuration file
*
*	@package	Kleophatra
*	@subpackage	/conf
*	@license	http://www.gnu.org/copyleft/gpl.html GNU/GPL License 2.0
*	@author		Sergio Milardovich <smilardovich@yahoo.com.ar>
*	@link		http://www.kleophatra.org
*/
	ob_start();
 	session_start();
	if (!defined("UNDERKLEO"))
	die("U must be UNDERKLEO ¬¬!");

	/*
	 * Development mode (You won't like to see this)
	 */
	 // define("KLEO_DEVELOPMENT_MODE",true,true);

	if(!defined('KLEO_DEVELOPMENT_MODE')){
		ini_set('display_errors','0');
	}

	/*
	 * - "Magic quotes are evil" -
	 * 			By Xiam
	 */
	set_magic_quotes_runtime(0);

	ini_set("memory_limit","16M");
	
	/* Source directory... */
	define("KLEO_SOURCE_DIR", substr(dirname(__FILE__),0,-5).'/');

	/* THE KLEO VERSION */
	define("KLEO_CURRENT_VERSION", "0.1.5");

	/* THE CODENAME */
	define("KLEO_CODENAME_VERSION", "Chuck Norris");

	/* Cookie name */
	define("KLEO_LOGIN_COOKIE_NAME", "Kleo_login"); // You can change it. Actually, you SHOULD change it :-).

	/* Cookie time */
	define("KLEO_LOGIN_COOKIE_TIME", time() + (7 * 86400)); // Default time is a week
  
/*
	// KLEO CONTENT TYPE
	define("KLEO_CONTENT_TYPE", $_SERVER['Content_Type']); // <-- BETA
*/
	/* If you are running kleo on a localhost, in development mode, uncomment this: */
	define("KLEO_LOCAL_MODE", true);


	/* Activate permalinks (ßeta since 0.1.5) */
	define("KLEO_USE_PERMALINKS", false, true);

	  /* SOME "DIR" VALUES */
  	define("KLEO_TEMPLATE_DIR", KLEO_SOURCE_DIR . "templates/");
	define("KLEO_MODULES_DIR", KLEO_SOURCE_DIR . "modules/");
	define("KLEO_PLUGINS_DIR", KLEO_SOURCE_DIR . "plugins/");
	define("KLEO_LIB_DIR", KLEO_SOURCE_DIR . "lib/");
	define("KLEO_LANG_DIR", KLEO_SOURCE_DIR . "lang/");
	define("KLEO_MEDIA_DIR", KLEO_SOURCE_DIR . "media/");
	define("KLEO_JS_DIR", KLEO_SOURCE_DIR . "js/");
	define("KLEO_CONFIG_DIR", KLEO_SOURCE_DIR . "conf/");
	define("KLEO_UPLOADS_DIR", KLEO_MEDIA_DIR."uploads/");

	/* FILES VALUES */
	define("KLEO_DB_CONFIG_FILE", KLEO_CONFIG_DIR . "db.php");

	/* SMART LANGAGE FILE SEARCH ENGINE */	
	define("KLEO_SMART_LANG", true, true);

	/* ENABLE THE PLUGINS? */
	define("KLEO_PLUGINS", true, true);
  
	  /*SOME VALUES*/
  	if (!isset($_SERVER["HTTP_USER_AGENT"])) {
      		$_SERVER["HTTP_USER_AGENT"] = "Unknown/:p";
  	}

	/* Tag separators... */
	define("KLEO_START_TAG_SEPARATOR", "", true);
	define("KLEO_END_TAG_SEPARATOR", ", ", true);

	/* Topic separators... */
	define("KLEO_START_TOPIC_SEPARATOR", "", true);
	define("KLEO_END_TOPIC_SEPARATOR", ", ", true);

	/* NO HTML? */
	//define("KLEO_NO_HTML", true, true); // <-- BETA

	/* LATEST VERSION */
	if(!KLEO_LOCAL_MODE){
		define("KLEO_LATEST_VERSION", file_get_contents("http://kleophatra.org/services/info.php?v"), true);
	} else {
		define("KLEO_LATEST_VERSION", KLEO_CURRENT_VERSION, true);
	}


	if(!file_exists(KLEO_DB_CONFIG_FILE) && basename($_SERVER['PHP_SELF']) !== 'install.php'){
		header("Location: install/install.php");
	}

	if(defined('KLEO_USE_PERMALINKS')){
		require_once KLEO_LIB_DIR.'permalinks.lib.php';
		$permalinks = new Permalinks();
		$permalinks->GetRoute();
		$permalinks->Redirect();
	}

	/* Some requires */
	require_once KLEO_DB_CONFIG_FILE;
	require_once KLEO_CONFIG_DIR.'db/'.KLEO_DB_DRIVER.'.php';
	require_once KLEO_LIB_DIR.'core.lib.php';
	require_once KLEO_LIB_DIR.'template.lib.php';
	require_once KLEO_LIB_DIR.'accounts.lib.php';
	require_once KLEO_LIB_DIR.'load.lib.php';
	require_once KLEO_LIB_DIR.'menus.lib.php';
	require_once KLEO_LIB_DIR.'modules.lib.php';
	require_once KLEO_LIB_DIR.'widgets.lib.php';
	require_once KLEO_LIB_DIR.'lang.lib.php';

	/* DB start */
	$db = new kleodb();
	/* Core functions */
	$kleocore = new kleocore();
	/* Load Lib Start */
	$kleoload = new kleoload();
	/* Widget Class */
	$kleowidgets = new kleowidgets();
	/* Modules Class */
	$kleomodules = new kleomodules();
	/* Groups And Users Class */
	$kleoaccounts = new kleoaccounts();
	/* Menus Class */
	$menus = new kleomenu();
	/* Language Class */
	$kleolang = new kleolang();

	/* Check for old cookies */
	$kleoaccounts->check_user();	

	/* Connect to the DB */
	if(!defined('KLEO_PERSISTENT_DB')){
		$db->connect(KLEO_DB_HOST, KLEO_DB_USER, KLEO_DB_PASS, KLEO_DB_NAME, KLEO_DB_PORT);
	} else {
		$db->pconnect(KLEO_DB_HOST, KLEO_DB_USER, KLEO_DB_PASS, KLEO_DB_NAME, KLEO_DB_PORT);
	}

	if(isset($_GET['style'])){
		if(file_exists(KLEO_TEMPLATE_DIR.$kleocore->get_core_value("web_template", "name").'/styles/'.$_GET['style'])){
			$_SESSION['style'] = $_GET['style'];
		}
	}

	if(isset($_GET['template'])){
		if(file_exists(KLEO_TEMPLATE_DIR.$kleocore->get_core_value("web_template", "name").'/tpl/main.tpl')){
			$_SESSION['template'] = $_GET['template'];
		}
	}
	/* Important Constants */
	define("KLEO_CURRENT_ICONS", $kleocore->get_core_value("web_icontheme", "name"));

	if(!isset($_SESSION['template'])){
		define("KLEO_CURRENT_TEMPLATE", $kleocore->get_core_value("web_template", "name"));
	} else {
		define("KLEO_CURRENT_TEMPLATE", $_SESSION['template']);
	}

	if(!isset($_SESSION['style'])){
		define("KLEO_CURRENT_STYLE", $kleocore->get_core_value("web_style", "name"));
	} else {
		define("KLEO_CURRENT_STYLE", $_SESSION['style']);
	}
	define("KLEO_WEB_TITLE", $kleocore->get_core_value("web_title", "name"));
	define("KLEO_WEB_NAME", $kleocore->get_core_value("web_name", "name"));
	define("KLEO_WEB_SLOGAN", $kleocore->get_core_value("web_slogan", "name"));
 	define("KLEO_DEFAULT_MODULE", $kleocore->get_core_value("default_module", "name"));
 	define("KLEO_DEFAULT_LANG", "en");
	define("KLEO_SITEURL", $kleocore->get_core_value("siteurl", "name"));

	define("KLEO_TEMPLATE_MEDIA", KLEO_SITEURL.'/templates/'.KLEO_CURRENT_TEMPLATE.'/styles/'.KLEO_CURRENT_STYLE.'/media/', true);

	define("KLEO_FULL_TEMPLATE_ROUTE", KLEO_TEMPLATE_DIR.$kleocore->get_core_value("web_template", "name").'/tpl/');
	define("KLEO_FULL_STYLE_ROUTE", KLEO_SITEURL.'/templates/'.$kleocore->get_core_value("web_template","name").'/styles/'.KLEO_CURRENT_STYLE.'/');
	
	define("KLEO_DEFAULT_AVATAR", "default.png", true); // You can choose between boba.png, clone.png, clone-old.png, darth.png, jango.png, r2d2.png and of course, our favourite: Tux (default.png)

	/* REPOSITORY VALUES */
	if(!defined("KLEO_REPOSITORY")){
		define("KLEO_REPOSITORY", $kleocore->get_core_value("repository", "name"), true);
	}

	/*
	 * Lang Constants...	
	 * Kleo search in this order:
	 * 1) Session
	 * 2) Browser
	 * 3) Defined
	 * 4) DB
	 */

	if(KLEO_SMART_LANG){

		// "Defined Method"
		if(defined('KLEO_LANG')){
			$lang = KLEO_LANG;
		}

		$lang = $kleocore->get_core_value("web_lang", "name");

		//Search for the browser accept language
		if($lang == ""){
			$browser_lang = explode("-", $_SERVER['HTTP_ACCEPT_LANGUAGE']);
			$lang = $browser_lang[0];
		}
		//Start the session
		if(isset($_GET['lang'])){
			if(file_exists(KLEO_LANG_DIR.$_GET['lang'].'/main.php')){
				$_SESSION['lang'] = $_GET['lang'];
			}
		}
		//If the session exists...
		if(isset($_SESSION['lang'])){
			$lang = $_SESSION['lang'];
		}
		define("KLEO_LANG", $lang);
	}

	if(isset($_SESSION['modules:values'])){
		$values = explode("/", $_SESSION['modules:values']);
		foreach($values as $value){
			$cut = explode("->", $value);
			$_GET[$cut[0]] = $cut[1];
		}
		unset($_SESSION['modules:values']);
	}
	if(!isset($_GET['module']) or !$kleomodules->is_active_module($_GET['module'])){
		$module = KLEO_DEFAULT_MODULE;
	} else if($kleomodules->is_active_module($_GET['module'])){
		$module = $_GET['module'];
	}

	define("KLEO_ACTUAL_MODULE", $module, true);

	if(KLEO_ACTUAL_MODULE == KLEO_DEFAULT_MODULE){
		define("KLEO_IS_HOME", $module, true);
	} else {
		define("KLEO_IS_HOME", false, false);
	}

	/* Language Settings... */
	if(!file_exists(KLEO_LANG_DIR.KLEO_LANG.'/main.php')){
		if(!file_exists(KLEO_LANG_DIR.KLEO_DEFAULT_LANG.'/main.php')){
		  $kleocore->apperror(L_KLEO_LANG_ERROR, 'common');
		} else {
		  include_once KLEO_LANG_DIR.KLEO_DEFAULT_LANG.'/main.php';
		}
	} else {
		include_once KLEO_LANG_DIR.KLEO_LANG.'/main.php';
	}

	// If the module has "language extension..."
	if(file_exists(KLEO_MODULES_DIR.$module.'/lang/'.KLEO_LANG.'/main.php')){
		include_once KLEO_MODULES_DIR.$module.'/lang/'.KLEO_LANG.'/main.php';
	} else if(file_exists(KLEO_MODULES_DIR.$module.'/lang/'.KLEO_DEFAULT_LANG.'/main.php')){
		include_once KLEO_MODULES_DIR.$module.'/lang/'.KLEO_DEFAULT_LANG.'/main.php';
	}
	// If the theme has a "Language extension..."
	if(file_exists(KLEO_TEMPLATE_DIR.KLEO_CURRENT_TEMPLATE.'/lang/'.KLEO_LANG.'/main.php')){
	  include_once KLEO_TEMPLATE_DIR.KLEO_CURRENT_TEMPLATE.'/lang/'.KLEO_LANG.'/main.php';
	} else if(file_exists(KLEO_TEMPLATE_DIR.KLEO_CURRENT_TEMPLATE.'/lang/'.KLEO_DEFAULT_LANG.'/main.php')){
	  include_once KLEO_TEMPLATE_DIR.KLEO_CURRENT_TEMPLATE.'/lang/'.KLEO_DEFAULT_LANG.'/main.php';
	}

	// Call the main variable of kleo: buff
	$buff = $kleoload->tpl_load(KLEO_FULL_TEMPLATE_ROUTE.'main.tpl', $buff);

	$kleoload->make_basics($buff, $buff);
	// Make the widgets
	for($i=1;$i<9;$i++){
	  $kleowidgets->make_widget($buff, $buff);
	}
	$kleowidgets->make_footer($buff, $buff);
	$kleomodules->load_module($module, $buff);
	$kleowidgets->compile($buff, $buff);

	/*
		if(defined(KLEO_NO_HTML)){
		$buff = htmlentities($buff);
	}
	*/

	/* 
	 * Que lindo es cuando todo anda como piña!
	 * esa sensación la tuve cuando hice el sistema de plugins :'-)
	 */
	if(KLEO_PLUGINS){
		$kleocore->apploadlib(KLEO_LIB_DIR.'plugins.lib.php', 'kleoplugins', $kleoplugins);
		$kleoplugins->load_plugins($buff, $buff);
	}

	$kleocore->apploadlib(KLEO_LIB_DIR."admin.lib.php", "kleoadmin", $actions);
	$actions->check_plugins();
	$actions->do_actions();

	/* 
	 * Get the user id 
	 */
	if(!isset($_SESSION['uid'])){
		$uid = 0;
	} else {
		$uid = $_SESSION['uid'];
	}

	/*
	 * Check the privilegies of the visitor 
	 * Comment this 3 lines to disable the privilegies (If you make it, the Sky will fall over your head)
	 */
	/*$kleoaccounts->log_visitor(); // still on ßETA TESTING */
	$kleoaccounts->get_group($uid,$group);
	$kleoaccounts->check_privilegies($group);
?>
